package com.kangda.circle;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.kangda.parttitionstorage.R;

/**
 * @Author Kang da
 * @Date 2019-11-21 16:01
 */
public class CirclePercentCircle extends View{
    private final int bgColor;
    private final int fgColor;
    private Context mContext;
    private int mArcWidth;
    private int mCenterTextColor;
    private int mCenterTextSize;
    private int mCircleRadius;
    private Paint arcPaint;
    private Paint arcCirclePaint;
    private Paint centerTextPaint;
    private RectF arcRectF;
    private Rect textBoundRect;
    private int mCurData = 0;
    private int allData;

    public CirclePercentCircle(Context context) {
        this(context, null);
    }

    public CirclePercentCircle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CirclePercentCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CirclePercentCircle, defStyleAttr, 0);
        mArcWidth = typedArray.getDimensionPixelSize(R.styleable.CirclePercentCircle_cpc_arcWidth, dip2px(context, 20));
        mCenterTextColor = typedArray.getColor(R.styleable.CirclePercentCircle_cpc_centerTextColor, ContextCompat.getColor(mContext, R.color.orange_ffcfab));
        mCenterTextSize = typedArray.getDimensionPixelSize(R.styleable.CirclePercentCircle_cpc_centerTextSize, dip2px(context, 20));
        mCircleRadius = typedArray.getDimensionPixelSize(R.styleable.CirclePercentCircle_cpc_circleRadius, dip2px(context, 100));
        bgColor = typedArray.getColor(R.styleable.CirclePercentCircle_cpc_cirBgColor, ContextCompat.getColor(mContext, R.color.orange_ffcfab));
        fgColor = typedArray.getColor(R.styleable.CirclePercentCircle_cpc_cirFgColor, ContextCompat.getColor(mContext, R.color.orange_ff8429));
        typedArray.recycle();
        initPaint();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private void initPaint() {

        arcCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcCirclePaint.setStyle(Paint.Style.STROKE);
        arcCirclePaint.setStrokeWidth(mArcWidth);
        arcCirclePaint.setColor(bgColor);
        arcCirclePaint.setStrokeCap(Paint.Cap.ROUND);

        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeWidth(mArcWidth);
        arcPaint.setColor(fgColor);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);

        centerTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        centerTextPaint.setStyle(Paint.Style.FILL);
        centerTextPaint.setColor(mCenterTextColor);
        centerTextPaint.setTextSize(mCenterTextSize);

        //圓弧的外接矩形
        arcRectF = new RectF();

        //文字的边界矩形
        textBoundRect = new Rect();

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureDimension(widthMeasureSpec), measureDimension(heightMeasureSpec));
    }

    private int measureDimension(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = mCircleRadius * 2;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.rotate(-90, getWidth() / 2.0f, getHeight() / 2.0f);
        arcRectF.set(getWidth() / 2.0f - mCircleRadius + mArcWidth / 2.0f, getHeight() / 2.0f - mCircleRadius + mArcWidth / 2.0f
                , getWidth() / 2.0f + mCircleRadius - mArcWidth / 2.0f, getHeight() / 2.0f + mCircleRadius - mArcWidth / 2.0f);


        canvas.drawArc(arcRectF, 0, 360, false, arcCirclePaint);
        if (allData != 0) {
            arcPaint.setColor(fgColor);
//        arcPaint.setShader(new SweepGradient(getWidth()/2,getHeight()/2,arcStartColor,arcEndColor));
            canvas.drawArc(arcRectF, 0, 360.0f * mCurData / allData, false, arcPaint);
        }
        canvas.rotate(90, getWidth() / 2.0f, getHeight() / 2.0f);
        String data = String.valueOf(mCurData).concat("/").concat(String.valueOf(allData));
        centerTextPaint.getTextBounds(data, 0, data.length(), textBoundRect);
        canvas.drawText(data, getWidth() / 2.0f - textBoundRect.width() / 2.0f, getHeight() / 2.0f + textBoundRect.height() / 2.0f, centerTextPaint);

    }

    public void setPercentData(int data, int allData, TimeInterpolator interpolator) {
        this.allData = allData;
        ValueAnimator valueAnimator = ValueAnimator.ofInt(mCurData, data);
        valueAnimator.setDuration((long) (Math.abs(mCurData - data) * 30.0f));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mCurData = (int) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setInterpolator(interpolator);
        valueAnimator.start();
    }
}
